<?php

/**
 * IndexController.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysblog\Controller;

use System\Common\SystemController;

class IndexController extends SystemController {
    public $limit = 20;

    public function index()
    {
        
        $this->system_title = '文章列表';
        // 处理搜索条件
        $search = array();
        $where = array();
        $this->_proSearch($search, $where);
        $page = rintval(I('get.p', 1));
        $start = null;
        $limit = $this->limit;
        list($start, $limit, $page) = page_limit($page, $limit);
        
        $obj_serv = D('Article', 'Service');
        $data = $obj_serv->getArticleList($where, '*', $start, $limit, 'aid desc');
        $count = $data['count'];
        $list = $data['list'];
        
        $this->_assignCate();
        $this->_assignPage($count, $limit, $search);
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->assign('search', $search);
        $this->assign('search_url',U('Index/index'));
        
        $this->loadSystemView();
    }
    
    public function add()
    {
        $this->system_title = '添加文章';
        $this->_assignUeditor();
        $this->_assignCate();
        $this->assign('act_url',U('Index/dosubmit'));
        
        return $this->loadSystemView('edit');
    }
    
    public function edit()
    {
        $this->system_title = '编辑文章';
        $id = I('get.aid');
        $obj_serv = D('Article','Service');
        try {
            $data = $obj_serv->getRow($id, 'aid,title,atid,node_id,ifpub,content');
            $data['id'] = $id;
            // 传递附件id
            if($data['atid'])
            {
                $this->assign('attids', $data['atid']);
            }
            $this->_assignUeditor(htmlspecialchars_decode($data['content']));
            $this->_assignCate();
            $this->assign('data', $data);
            $this->assign('act_url',U('Index/dosubmit'));
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
        
        return $this->loadSystemView();
    }
    
    public function dosubmit()
    {
        $request = I('post.');
        $obj_serv = D('Article','Service');
        try {
            if(!$request)
            {
                throw new \Exception('保存失败');
            }
            $this->__proSaveData($request, $post_data);
            
            $rs = $obj_serv->saveData($post_data);
            if(!$rs)
            {
                throw new \Exception('保存失败');
            }
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            return $this->error($msg);
        }
        
        return $this->success('保存成功',U('Index/index'));
    }
    
    // 删除文章
    public function del()
    {
        $aid = I('get.aid');
        $obj_serv = D('Article','Service');
        try {
            $rs = $obj_serv->delRow($aid);
            if(!$rs)
            {
                throw new \Exception('删除失败');
            }
        } catch (\Exception $e) {
            $msg = $e->getMessage();
            return $this->error($msg);
        }
        
        return $this->success('删除成功');
    }
    
    // 获取分类
    protected function _assignCate()
    {
        $obj_serv_node = D('Category', 'Service');
        $cat_list = $obj_serv_node->getCatList('node_id,node_name,parent_id');
        $cat_list = $obj_serv_node->proCatList($cat_list);
        $this->assign('catlist', $cat_list);
        
        return true;
    }
    protected function _proSearch(&$request, &$where)
    {
        $request = I('get.');
        if(!$request)
        {
            return false;
        }
        $this->__proFilter($request);
        if(isset($request['title']) && $request['title'])
        {
            $where['title'] = array('LIKE', '%'.$request['title'].'%');
        }
        
        if(isset($request['cat_id']) && $request['cat_id'] && $request['cat_id'] > 0)
        {
            $where['node_id'] = intval($request['cat_id']);
        }
        
        return true;
    }
    
    private function __proFilter(&$search)
    {
        if(!$search || !is_array($search))
        {
            return false;
        }
    
        foreach ($search as &$val)
        {
            $val = urldecode($val);
        }
    
        return true;
    }
    
    private function __proSaveData($request, &$post_data)
    {
        if(!$request)
        {
            return false;
        }
        
        if(isset($request['id']) && $request['id'])
        {
            $post_data['aid'] = $request['id'];
        }
        
        if(isset($request['article-title']))
        {
            $post_data['title'] = $request['article-title'];
        }
        
        if(isset($request['content']))
        {
            $post_data['content'] = $request['content'];
        }
        
        if(isset($request['article-actid']))
        {
            $post_data['node_id'] = $request['article-actid'];
        }
        
        if(isset($request['attids']))
        {
            $post_data['atid'] = $request['attids'];
        }
        
        if(isset($request['"is_publish"']))
        {
            $post_data['ifpub'] = $request['is_publish'];
        }
        
        return true;
    }

}
 